package com.rjwh.dingdong.client.util;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Kay
 * @ClassName PermissionUtil
 * @Description
 * @date 2016-8-29
 * @history 2016-8-29 author: description:
 */
public class PermissionUtil {
    private static PermissionUtil permissionUtil = null;


    private HashMap<String, String> mListPermissions = new HashMap<String, String>();

    private PermissionUtil() {

        mListPermissions.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, "访问存储");
        mListPermissions.put(Manifest.permission.READ_PHONE_STATE, "手机状态");
        mListPermissions.put(Manifest.permission.ACCESS_COARSE_LOCATION, "位置信息");
        mListPermissions.put(android.Manifest.permission.CAMERA, "相机设备");

    }


    /**
     * 单例模式初始化
     * author LH
     * data 2016/7/27 11:27
     */
    public static PermissionUtil getInstance() {
        if (permissionUtil == null) {
            permissionUtil = new PermissionUtil();
        }
        return permissionUtil;
    }

    /**
     * 判断当前为M以上版本
     * author LH
     * data 2016/7/27 11:29
     */
    public boolean isOverMarshmallow() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
    }

    /**
     * 获得没有授权的权限
     * author LH
     * data 2016/7/27 11:46
     */
    @TargetApi(value = Build.VERSION_CODES.M)
    public HashMap<String, String> findDeniedPermissions(Activity activity) {

        HashMap<String, String> denyPermissions = new HashMap<>();
        Iterator iter = mListPermissions.entrySet().iterator();

        // the first method to travel the map
        while (iter.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();

            String key = entry.getKey();
            String value = entry.getValue();

            if (activity.checkSelfPermission(key) != PackageManager.PERMISSION_GRANTED) {
                denyPermissions.put(key, value);
            }

        }

        return denyPermissions;
    }

    public interface PermissionCallBack {
        void onPermissionGetSuccess();

        void onPermissionGetFail();

    }
}
